Speeding Up CFML Development With Dreamweaver MX 2004: How To Create Custom Server Behaviors and Dreamweaver Extensions
For many ColdFusion developers including myself, the shift from using ColdFusion Studio to using Macromedia Dreamweaver MX for CFML development has been anything but easy. Gone are the familiar keyboard shortcuts, context sensitive help, "resource window" tabs on the left-hand side, and all the other things we love about CFStudio... or are they? With the release of Macromedia Studio MX 2004, Dreamweaver now offers much of the functionality and flexibility ColdFusion Studio users have grown accustomed to, such as snippets with keyboard shortcuts, context-sensitive help, and so forth. This is dandy, but if all Dreamweaver offers developers is a new look to old functionality, it wouldn't be much of a step in the right direction, would it? Though I never thought I'd say it, the truth is that the more I use Dreamweaver MX 2004, the more I am convinced that it offers developers benefits that are superior to anything we ever had with ColdFusion Studio. One such feature is the server behavior framework for extending the Dreamweaver authoring environment. In this article, I will examine Dreamweaver Extensions and server behaviors; what they are and how (and why) to create them.
Requirements
Macromedia Dreamweaver MX 2004
Macromedia ColdFusion MX 6.1
What are Dreamweaver Server Behaviors?
To put it simply, server behaviors are pre-written chunks of text that you can insert into your page in Dreamweaver with a few mouse clicks. If you are a former ColdFusion Studio user, this probably sounds an awful lot like a snippet, doesn't it? In some respects they aren't very different, although there's a lot more to server behaviors than just being a chunk of pre-written text. The major differences between a server behavior and a snippet are:
- Server behaviors can be parameterized.
- Dreamweaver recognizes server behaviors in your code.
- You can package server behaviors as a Dreamweaver extensions.
In Dreamweaver MX 2004, developers can create a snippet of text and associate a keyboard shortcut with that snippet so they can quickly insert it into pages. For very static text, this is fine. However, often times you must customize the code in a snippet in order for it to be accurate for the page you are inserting it into. By using a server behavior, you can parameterize that piece of text and prompt developers for the parameter values before Dreamweaver inserts the text snippet. What's more, the user interface presented to users is not limited to simple text input controls to capture values. You can present them with browse buttons, data source pop-up menus, and many other robust form field types.
To parameterize a snippet of text in ColdFusion Studio, you had to learn VTML, its proprietary UI scripting language. To extend ColdFusion Studio with more robust functionality, you had to learn VTOM, a javascript-like scripting language. Similarly, in order to extend Dreamweaver with your own functionality, you can extend the environment by writing JavaScript. Under the hood, Dreamweaver exposes its interface, configuration settings, the current document, and more through a special javascript DOM. Unlike ColdFusion Studio, Dreamweaver offers developers the ability to extend the environment without having to learn any new technology or scripting language with its Server Behavior Builder utility. Most ColdFusion developers place a comment block containing information such as the author name, supported ColdFusion version, file name, purpose, and description, and so forth. at the top of every page they create. This repetitive task can be made simple and the comments at the top of every page are standard by creating a page comment server behavior. We will start with a typical comment block as follows:
<!--- ::
* Application: My Application
* Supported CF Version(s): MX 6.1
* File Name: myFile.cfm
* Section: application section
* Creator: Simon Horwith
* Creation Date: 10/28/2003
* Purpose:
* Description:
* Version History & Comments
*
*
:: --->
As I mentioned earlier, if all you want is to insert this text at the top of your pages, a snippet will do. However, perhaps you are a consultant or you work on many different applications, possibly running on a variety of ColdFusion application server versions. Every time you use the snippet, you would then have to manually "correct" its information. I will walk through the steps to turn this snippet of text into a server behavior that will ask you for the appropriate values before putting the comment into your document.
-
To begin, open any CFML page with Dreamweaver MX (or create a new CFML page). You must open a file to use the server behavior panel. You will find server behaviors tab within the Application panel in Dreamweaver, as shown in Figure 1.
Figure 1. The server behaviors tab in the Application panel
-
To add a new server behavior, click the plus (+) button on the server behaviors tab and select "New Server Behavior…" from the menu. A dialog box similar to Figure 2 will open.
Figure 2. Create New Server Behavior
- This is a ColdFusion comment, so leave the Document Type pop-up menu on its default of ColdFusion.
- Change the text in the Name text box to Page Comment and leave the option for copying an existing server behavior unchecked.
-
Click OK to continue. Dreamweaver now displays a dialog box similar to Figure 3. In this dialog box, you can add all of the text blocks that the server behavior will insert into a document, define the editable regions of the text block(s), and specify for Dreamweaver where and how in the current document to insert the text block(s).
Figure 3. Define Server Behavior Text Blocks
- Click the plus (+) button in the upper left corner of the dialog box (shown in Figure 3) to add a text block to the server behavior. A dialog box asks you to specify the name of this text block.
- Rename the text block or leave the default value and then click OK.
-
After clicking the OK button to assign a name to the new text block, it returns you to the define server behavior dialog window (Figure 3). Dreamweaver has added the new text block and displays it in the text area at the top of the window. The default text for this block of text is displayed in "code block" text area below it. The "Insert Parameter in Code Block" button and "Insert Code" pop-up menu are available and a "Relative Position" pop-up menu and "Position" text box are available (Figure 4).
Figure 4. Server Behavior Builder immediately after adding a text block
-
Whenever you use the server behavior, you must output the comment block; copy the comment block listed above to your clipboard and paste it into the Code Block text area, replacing the default text.
Once the server behavior has inserted the text into documents into the code block text area, you must which text area(s) the user will determine dynamically by his/her input. In the page comment example, the application name, ColdFusion version, file name, section of the application, create date, purpose, and description will be dynamic. If the server behavior were intended for use in a single application, the application name would not be dynamic. The author name will not be dynamic because the author will always be you.
-
To flag the application name as dynamic, highlight the area where the user-supplied application name should be inserted; in this case, it's the text My Application (Figure 5).
Figure 5. Designating a dynamic region in the text block
- Selecting your dynamic region, click the Insert Parameter in Code Block button and assign the unique name appname to this region and click OK. After clicking OK, notice that the text in the block is renamed to @@appname@@.
-
Repeat this process for all of the remaining regions you plan to make dynamic.
Note: Be sure to give the regions the exact same names shown in Figure 7 below. Specifically, you need to use the names filename, appsection, and createdate for those respective parameter names. The names are case sensitive. Failure to name these correctly will cause errors in the form field auto-population JavaScript that I cover later in the article.
Because this is a comment block that is supposed to be at the very top of a document, leave the Insert Code pop-up menu on its default value (Above the
<html>tag) and change the Relative Position pop-up menu to The Beginning of the File. -
Click Advanced to reveal a checkbox that tells Dreamweaver to identify this block of text in a document and display it in the Server Behaviors panel if present. Put a check in this checkbox. Also change "Code block to select" to the name of your code block. The dialog box now looks like Figure 6.
Figure 6. Ready to continue building the server behavior
-
Once you have set the dynamic regions and insertion behavior (Figure 6), click Next. The Generate Server dialog box displays. In this dialog box, specify the User Interface to display for each of the dynamic regions of the text block that this server behavior inserts into a document. It contains a table with each of the parameter names along with its UI type displayed in a row. There are also up and down arrows for positioning the input parameters in the form window that will display when you run the server behavior. Selecting any row displays an arrow on the far right, in which you can assign a UI Type to the parameter, as shown in Figure 7.
Figure 7. Assign UI Types to the Server Behavior Parameters
The default UI Interface for parameters is Text Field, which is fine for all but the ColdFusion Version parameter. Technically, a text field would suffice for this parameter, but selecting the ColdFusion Version from a pop-up menu is more useful.
- Click the cfversion entry in your table, click the arrow on the right, and select List Menu from the menu (Figure 7). Once you've assigned your desired UI Interface types to the parameters, click OK.
- To test the new server behavior, click the plus (+) button on the server behaviors tab (Figure 1). There should now be a Page Comment entry in the menu.
- Click the Page Comment menu entry to test the server behavior.
Dreamweaver displays a form for you to specify each parameters with one exception. The ColdFusion Version pop-up menu doesn't have any options aside from "***No values specified". The form itself may also need some cleaning-up. This is OK. In addition to cleaning up the form and adding options to the select list, it would be useful to pre-fill the filename box with the file name the user is currently editing, pre-fill the application section box with the path to the current document, and to pre-fill the create date text box with today's date (Figure 8). You can do all of this with a little bit of HTML and JavaScript knowledge!
On Windows, store the files that define a ColdFusion server behavior in "{drive_letter}:\Documents and Settings\{username}\Application Data\Macromedia\Dreamweaver MX 2004\Configuration\ServerBehaviors\ColdFusion." On Macintosh, store the files in "HD|Applications|Macromedia Dreamweaver MX 2004|Configuration|ServerBehaviors|ColdFusion." There you will find an EDML file for the behavior, text block, and HTML file ("Page Comment.htm" in this example). The EDML files define which text block(s) are part of the behavior, which text block(s) layout(s) are, and where to position dynamic parameters. The HTML file defines the actual UI, which is what you want to modify. Use the following steps to do this.
- Open Page Comment.htm in Dreamweaver or any other text editor. This file contains mostly javascript and the HTML for the form at the bottom.
- Find the
<select>tag for the ColdFusion versions, and delete the current option and add your own options, one for each ColdFusion Version you want to appear in the pop-up menu. - To default all of the values in the other form fields, change the
<BODY>tag onload event value to:javascript:initializeUI();defaultFields(); -
Add the following JavaScript immediately following the first
<SCRIPT>block on the page:<SCRIPT> function defaultFields(){ var tmpDate = new Date(); var defaultDate = (tmpDate.getMonth() + 1) + '/' + tmpDate.getDate() + '/' +tmpDate.toString().substr(tmpDate.toString().length - 4, 4); var tmpSiteLen = dreamweaver.getSiteRoot().length; var tmpFilePath = dreamweaver.getDocumentPath("document"); var tmpPath = tmpFilePath.substr(tmpSiteLen, tmpFilePath.length - tmpSiteLen); var aPath = tmpPath.split("/"); var defaultFile = ""; var defaultSection = ""; if (aPath.length == 1){ defaultSection = "root"; } else{ for (i = 0; i < aPath.length - 1; i++){ defaultSection += aPath[i] + "/"; } defaultSection = defaultSection.substr(0, defaultSection.length - 1); } defaultFile = aPath[aPath.length - 1]; document.forms[0].createdate.value = defaultDate; document.forms[0].filename.value = defaultFile; document.forms[0].appsection.value = defaultSection; } </SCRIPT>
Figure 8. A little bit of HTML and JavaScript and now this is the UI!
Without getting into too much of the nitty-gritty detail, this is some simple javascript to calculate and assign the default values for the text controls. One thing you'll notice is the use of a "Dreamweaver" object in the JavaScript. This is one of the objects added to the DOM that Dreamweaver extension JavaScript has access to. You can learn much more about this DOM (and about extending Dreamweaver with other technologies) in the Dreamweaver API reference. You can read more about this in the documentation at Help > Extensions > API Reference in Dreamweaver. You can also learn about server behaviors and other ways to extend Dreamweaver with the Extending Dreamweaver reference (Help > Extensions > Extending Dreamweaver).
Where To Go From Here?
In addition to the references and utilities that come with Dreamweaver, with a little bit of research and practice, you can extend the Dreamweaver functionality pretty much any way you desire. You can even take your extensions and package them for distribution through the Macromedia Exchange. This requires very little work once you create the extension locally. Simply copy the EDML and HTML files for your extension to a separate directory and add a new file to that directory with an MXI extension. You can find MXI examples in the Macromedia Extension Manager installation directory and you can also learn more about how to define the installation package in an MXI file here.
After creating the MXI file, which is really nothing more than a relatively simple XML file, double-click it, which launches the Extension Manager Compiler and click OK. The MXP file is then created and ready for you to distribute or to upload to the Dreamweaver Exchange. It's really not too difficult at all. Even if all this talk about JavaScript and HTML configuration files scares you, the truth is that the visual interface to the server behavior builder offers most of the functionality you need to create and use local Dreamweaver extensions, and it doesn't require any coding at all! There's really no excuse not to click around and try creating some behaviors yourself for a half-hour. Who knows, you may like it. You can't break anything… if you don't like a behavior you create, simply delete it with the Edit Server Behaviors option in the main Server Behaviors panel menu.